<template>
  <el-card class="list-container">
    <!-- <h1>菜单列表</h1> -->
    <el-button
      @click="$router.push('/menu/add')"
      type="primary"
      icon="el-icon-circle-plus-outline"
      >添加</el-button>
    <el-divider> </el-divider>
    <el-table
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" min-width="80"> </el-table-column>
      <el-table-column
        prop="title"
        label="姓名"
        min-width="180"
      ></el-table-column>
      <el-table-column prop="icon" label="图标" min-width="180">
        <template v-slot="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="url"
        label="地址"
        min-width="180"
      ></el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="scope">
          <!-- 1:正常  2:禁用 -->
          <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template slot-scope="$props">
          <el-button
            type="success"
            @click="edit($props.row.id)"
            size="mini"
            circle
            icon="el-icon-edit"
          ></el-button>
          <el-button
            type="danger"
            @click="del($props.row.id)"
            size="mini"
            circle
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { getMenuList } from "../../request/menu";
export default {
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    del(id) {
      this.$confirm(() => {
        this.$http.post("/api/menudelete", { id }).then((res) => {
          if (res.code == 200) {
            const index = this.list.findIndex((item) => item.id == id);
            this.list.splice(index, 1);
            this.$success("删除成功");
          } else {
            this.$error(res.msg);
          }
        });
      });
    },
    edit(id) {
      this.$router.push(`/menu/${id}`);
    },

    getMenuList() {
      this.$http.get("/api/menulist?istree=1", {}).then((res) => {
        console.log(res);
        if (res.code === 200) {
          this.list = res.list;
        }
      });
    },
    // async getMenuList() {
    //   const list = await getMenuList();
    //   if (list) {
    //     this.list = list;
    //   }
    // },
  },
};
</script>

<style>
</style>